<?php
/**
 * Anniversary.php
 * 文件描述 纪念日控制器
 * Created on 2020/8/31 11:46
 * Created by 刘小龙 595929049@qq.com
 */

namespace app\xiangji\controller;


use app\common\helpers\Response;
use app\Request;
use app\xiangji\BaseController;
use app\xiangji\services\LoveDiaryService;
use think\App;
use think\exception\ValidateException;

class LoveDiary extends BaseController
{
    private $diaryService;

    public function __construct(LoveDiaryService $diaryService)
    {
        $this->diaryService = $diaryService;
    }

    public function fetch(Request $request)
    {
        try {
            $this->validate($request->get(['page', 'limit', 'date', 'content', 'only']), [
                'page|页码' => 'require|integer|min:1',
                'limit|每页条数' => 'require|integer|min:1',
                'date|日期' => 'dateFormat:Y-m-d',
                'only|查看人员' => 'in:0,1,2',
            ]);
            $result = $this->diaryService->fetch($request);
            return $this->success($result);
        } catch (ValidateException $exception){
            return $this->failed($exception->getMessage(), Response::HTTP_BAD_REQUEST);
        }
    }

    public function create(Request $request)
    {
        $result = $this->diaryService->create($request);
        return $this->success($result);
    }

    public function update(Request $request, $id)
    {
        $result = $this->diaryService->update($request, $id);
        return $this->success($result);
    }

    public function delete($id)
    {
        $result = $this->diaryService->delete($id);
        return $this->success($result);
    }

    public function show($id)
    {
        $result = $this->diaryService->show($id);
        return $this->success($result);
    }

    public function countByMonth(Request $request)
    {
        try {
            $month = $request->get('month');
            $this->validate(['month' => $month], ['month|月份'  => 'require|dateFormat:Y-m']);
            $result = $this->diaryService->countMonthDiaries($month);
            return $this->success($result);
        } catch (ValidateException $exception){
            return $this->failed($exception->getMessage(), Response::HTTP_BAD_REQUEST);
        }
    }
}
